import { Module } from '@nestjs/common';
import { EmailService } from './email.service';
import { EmailController } from './email.controller';
import { MailerModule } from '@nestjs-modules/mailer';
import { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter';
import * as path from 'path';
import { ConfigService } from '@nestjs/config';
import { EnvEnum } from '../enums/env.enum';

@Module({
  imports: [
    MailerModule.forRootAsync({
      useFactory: (configService) => {
        return {
          transport: {
            host: 'smtp.qq.com', //邮箱服务器地址
            port: 587, //服务器端口
            secure: false,
            auth: {
              user: configService.get(EnvEnum.EMAIL_USER),
              pass: configService.get(EnvEnum.EMAIL_PASS)
            }
          },
          // preview: true,//是否开启预览，开启了这个属性，在调试模式下会自动打开一个网页，预览邮件
          defaults: {
            from: {
              name: '一诺滚雪球',
              address: configService.get(EnvEnum.EMAIL_USER)
            }
          },
          template: {
            dir: path.join(process.cwd(), './src/common/email/template'),//ejs模板文件夹路径
            adapter: new EjsAdapter(),
            options: {
              strict: true //严格模式
            }
          }
        }
      },
      inject: [ConfigService]

    })
  ],
  controllers: [EmailController],
  providers: [EmailService],
})
export class EmailModule { }
